//
//  GRBankCertificationViewController.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/23.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRBankCertificationViewController.h"
#import "GRSafetyCertificateViewController.h"
//#import "GRPayDetailViewController.h"

#import "GRCertificationHeader.h"
#import "GRCertificationFooter.h"
#import "GRCertificationViewCell.h"

@interface GRBankCertificationViewController () <GRCertificationProtocol>

{
    NSArray *titleArray, *keyArray;
    
}
@property (nonatomic, strong) GRCertificationFooter *footer;

@end

@implementation GRBankCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleArray = @[@"持卡人", @"银行卡号", @"银行名称", @"手机号"];
    keyArray   = @[@"name", @"bankCardNumber", @"bankName", @"bankMobile"];
    
    NSString *mobile = [GRUserBasisInfo UserBasis].mobile;
    [self.paramers setValue:mobile forKey:@"bankMobile"];
    // Uncomment the following line to preserve selection between presentations.
    GRCertificationHeader *header  = [[GRCertificationHeader alloc] initWithCertification:GRCertificationBankCard];
    header.delegate                = self;
    header.currentVc               = self;
    self.tableView.tableHeaderView = header;
    
    self.tableView.tableFooterView = self.footer;

    self.tableView.rowHeight       = 45;
    self.tableView.backgroundColor = HEXCOLOR(0xf8f8f8);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRCertificationViewCell" bundle:nil] forCellReuseIdentifier:@"GRCertificationViewCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRCertificationViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"GRCertificationViewCell" forIndexPath:indexPath];
    cell.titleLabel.text           = titleArray[indexPath.row];
    NSString *key                  = keyArray[indexPath.row];
    if (self.paramers[key]) {
        cell.infoTextField.text    = self.paramers[key];
    }else {
        cell.infoTextField.placeholder = @"拍照识别";
    }
    return cell;
}


#pragma Mark ----------- 提交认证 ----------------

-(void)didselectConfirmAction:(UIButton *)sender {
    sender.enabled = NO;
    NSArray *keys  = @[@"bankUseName", @"bankNumber", @"bankName", @"bankMobile"];
    for (int i = 0; i < titleArray.count; i++) {
        GRCertificationViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        [self.paramers setValue:cell.infoTextField.text forKey:keys[i]];
    }
    WEAKSELF
    [GRCertificationAction submitPersonalCertification:self.paramers completionHandler:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            BOOL ispass = NO;
            if (responseObject[@"isPass"]) {
                ispass  = [responseObject[@"isPass"] boolValue];
            }
            [weakSelf goBackDesignatedController:ispass];
        }else {
            [GRHUD showErrorMsg:[error errorMsg]];
        }
        sender.enabled = YES;
    }];
}

- (void)goBackDesignatedController:(BOOL)isAuth {
    BOOL isBackToPay    = NO;
//    for (UIViewController *vc in self.navigationController.viewControllers) {
//        if ([vc isKindOfClass:[GRPayDetailViewController class]]) {
//            isBackToPay = YES;
//            break;
//        }
//    }
//
    for (UIViewController *designatedVc in self.navigationController.viewControllers) {
        if (isBackToPay && isAuth) {
//            if ([designatedVc isKindOfClass:[GRPayDetailViewController class]]) {
//                [(GRPayDetailViewController *)designatedVc passRealNameAuth];
//                [self.navigationController popToViewController:designatedVc animated:YES];
//                break;
//            }
        }else {
            if ([designatedVc isKindOfClass:[GRSafetyCertificateViewController class]]) {
                [(GRSafetyCertificateViewController *)designatedVc getCertification];
                [self.navigationController popToViewController:designatedVc animated:YES];
                break;
            }
        }
    }
}


-(void)getIdentificationInfomation:(NSDictionary *)response {
    [self.paramers addEntriesFromDictionary:response];
    [self changeNextBtnStatus:YES];
    [self.tableView reloadData];
}


- (void)changeNextBtnStatus:(BOOL)next {
    _footer.sureBtn.enabled = next;
    _footer.sureBtn.backgroundColor = next ? KGMTHEM_COLOR : HEXCOLOR(0xeeeeee);
    [_footer.sureBtn setTitleColor:next ? KGMBLACK_COLOR : HEXCOLOR(0x999999) forState:UIControlStateNormal];
}



/**
 懒加载

 @return <#return value description#>
 */
-(GRCertificationFooter *)footer {
    if (!_footer) {
        _footer = [[GRCertificationFooter alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, 65)];
        _footer.delegate = self;
        [self changeNextBtnStatus:NO];
        [_footer.sureBtn setTitle:@"提交" forState:UIControlStateNormal];
    }
    return _footer;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
